//
//  ZGSOptionViewController.m
//  MapleLeaf
//
//  Created by 邝子涵 on 15/12/8.
//  Copyright © 2015年 AntennaBabyTeam. All rights reserved.
//

#import "ZGSOptionViewController.h"
#import "ZGSOptionModel.h"

@interface ZGSOptionViewController ()<UITableViewDataSource, UITableViewDelegate, UIActionSheetDelegate, UIAlertViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UILabel *cacheSize;
@property (nonatomic, retain) NSString *cacheSizeStr;
@property (nonatomic, retain) UISlider *slider;
@property (nonatomic, retain) UILabel *handTypeLable;
@property (nonatomic, retain) UISwitch *readSwitch;

@property(nonatomic, strong) ZGSOptionModel *set;
@end

@implementation ZGSOptionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(setSystemAction:)];
    
    self.cacheSizeStr = @"清除缓存";
    // 个人设置标题背景View
    UIView *firstHeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 50 * HEIGHT / 667.0)];
    firstHeadView.backgroundColor = [UIColor colorWithRed:0.965f green:0.965f blue:0.965f alpha:1.00f];
    [self.view addSubview:firstHeadView];
    
    // 个人设置标题
    UILabel *firstHeadLabel = [[UILabel alloc] initWithFrame:CGRectMake(15 * WIDTH / 375.0, 15 * HEIGHT / 667.0, 100, 20 * HEIGHT / 667.0)];
    firstHeadLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
    firstHeadLabel.text = @"个人设置";
    [firstHeadView addSubview:firstHeadLabel];

    
    // 设置背景亮度进度条
    // View
    UIView *lightBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 40 * HEIGHT / 667.0, WIDTH, 100 * HEIGHT / 667.0)];
    [self.view addSubview:lightBgView];
    // 图片
    UIImageView *lightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(8 * WIDTH / 375.0, 17 * HEIGHT / 667.0, 40 * HEIGHT / 667.0, 40 * HEIGHT / 667.0)];
    lightImageView.image = [UIImage imageNamed:@"iconfont-liangdu.png"];
    [lightBgView addSubview:lightImageView];
    
    
    // 调节阅读亮度
    UILabel *lightTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(60 * WIDTH / 375.0, 25 * HEIGHT / 667.0, 150, 20 * HEIGHT / 667.0)];
    lightTitleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    lightTitleLabel.text = @"阅读亮度调节";
    [lightBgView addSubview:lightTitleLabel];
    
    
    // 调节亮度条
    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(15 * WIDTH / 375.0, 65 * HEIGHT / 667.0, WIDTH - 30, 20 * HEIGHT / 667.0)];
    [self.slider setThumbImage:[UIImage imageNamed:@"iconfont-shijiandian.png"] forState:UIControlStateNormal];
    self.slider.value = self.set.light;
    self.slider.minimumValue = 0.04;
    self.slider.maximumValue = 0.5;
    [lightBgView addSubview:self.slider];
    
    
    // 调节亮度进度条
    UIView *lightLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 99 * HEIGHT / 667.0, WIDTH, 1)];
    lightLineView.layer.borderWidth = 1;
    lightLineView.layer.borderColor = [[UIColor colorWithRed:0.871f green:0.871f blue:0.871f alpha:1.00f] CGColor];
    [lightBgView addSubview:lightLineView];

    
    
    // 惯用手设置
    // 惯用手背景View设置
    UIView *handTypeBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 140 * HEIGHT / 667.0, WIDTH, 70 * HEIGHT / 667.0)];
    [self.view addSubview:handTypeBgView];
    // 惯用手图片
    UIImageView *handImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15 * WIDTH / 375.0, 20 * HEIGHT / 667.0, 30 * HEIGHT / 667.0, 30 * HEIGHT / 667.0)];
    handImageView.image = [UIImage imageNamed:@"iconfont-shouxing.png"];
    [handTypeBgView addSubview:handImageView];

    // 惯用手名字
    UILabel *handTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(60 * WIDTH / 375.0, 35 * HEIGHT / 667.0 - 10, 180 * WIDTH / 375.0, 20)];
    handTitleLabel.text = @"设置惯用手";
    handTitleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    [handTypeBgView addSubview:handTitleLabel];
    
    
    // 
    self.handTypeLable = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 90 * WIDTH / 375.0, 35 * HEIGHT / 667.0 - 10, 80 * WIDTH / 375.0, 20)];
    // 因BOOL类型默认为NO,所以起始为左手模式
    if (self.set.isRightHandType == YES) {
        self.handTypeLable.text = @"右手模式";
    } else {
        self.handTypeLable.text = @"左手模式";
    }
    [handTypeBgView addSubview:self.handTypeLable];
    
    // 给View添加单击手势,点击之后弹出UIActionSheet
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(setHandTypeAction:)];
    [handTypeBgView addGestureRecognizer:tap];
    
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    
    
    
    
    UIView *handTypeLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 69 * HEIGHT / 667.0, WIDTH, 1)];
    handTypeLineView.layer.borderWidth = 1;
    handTypeLineView.layer.borderColor = [[UIColor colorWithRed:0.871f green:0.871f blue:0.871f alpha:1.00f] CGColor];
    [handTypeBgView addSubview:handTypeLineView];
    
    
    // 阅读设置
    UIView *readTypeBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 210 * HEIGHT / 667.0, WIDTH, 70 * HEIGHT / 667.0)];
    [self.view addSubview:readTypeBgView];
    
    UIImageView *readImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15 * WIDTH / 375.0, 20 * HEIGHT / 667.0, 30 * HEIGHT / 667.0, 30 * HEIGHT / 667.0)];
    readImageView.image = [UIImage imageNamed:@"iconfont-tixing.png"];
    [readTypeBgView addSubview:readImageView];
    
    
    UILabel *readTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(60 * WIDTH / 375.0, 35 * HEIGHT / 667.0 - 10, 180 * WIDTH / 375.0, 20)];
    readTitleLabel.text = @"移动网络下阅读提醒";
    readTitleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    [readTypeBgView addSubview:readTitleLabel];
    
    
    self.readSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(WIDTH - 60 * WIDTH / 375.0, 35 * HEIGHT / 667.0 - 15, 45 * WIDTH / 375.0, 30)];
    self.readSwitch.onTintColor = [UIColor colorWithRed:1.000f green:0.267f blue:0.000f alpha:1.00f];
    self.readSwitch.on = self.set.isOpenReadRemind;
    [readTypeBgView addSubview:self.readSwitch];
    [self.readSwitch addTarget:self action:@selector(changeReadTypeAction:) forControlEvents:UIControlEventValueChanged];
    
    UIView *readTypeLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 69 * HEIGHT / 667.0, WIDTH, 1)];
    readTypeLineView.layer.borderWidth = 1;
    readTypeLineView.layer.borderColor = [[UIColor colorWithRed:0.871f green:0.871f blue:0.871f alpha:1.00f] CGColor];
    [readTypeBgView addSubview:readTypeLineView];

    
    
    // 其他标题
    UIView *secHeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 280 * HEIGHT / 667.0, WIDTH, 50 * HEIGHT / 667.0)];
    secHeadView.backgroundColor = [UIColor colorWithRed:0.965f green:0.965f blue:0.965f alpha:1.00f];
    [self.view addSubview:secHeadView];
    
    UILabel *secHeadLabel = [[UILabel alloc] initWithFrame:CGRectMake(15 * WIDTH / 375.0, 15 * HEIGHT / 667.0, 100, 20 * HEIGHT / 667.0)];
    secHeadLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
    secHeadLabel.text = @"其他";
    [secHeadView addSubview:secHeadLabel];

    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 330 * HEIGHT / 667.0, WIDTH, 205 * HEIGHT / 667.0) style:UITableViewStylePlain];
    self.tableView.rowHeight = 70 * HEIGHT / 667.0;
    self.tableView.separatorStyle = NO;
    [self.view addSubview:self.tableView];
    
    
    
    [self.tableView registerClass:[ZGSOptionCell class] forCellReuseIdentifier:@"oReuse"];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    
//    [self getCacheSize];
}

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.961f green:0.325f blue:0.173f alpha:1.00f];
    self.title = @"系统设置";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor whiteColor], NSForegroundColorAttributeName , nil]];
//    [self getData];
}

// 保存信息
- (void)setSystemAction:(UIBarButtonItem *)button {
    self.set = [[ZGSOptionModel alloc] init];
    //  把当前slider值赋值给Model进行存储
    self.set.light = self.slider.value;
    
    
    // 判断根据上面BOOL值默认为0 所以把当前值通过BOOL值存储到Model
    if ([self.handTypeLable.text isEqualToString:@"左手模式"]) {
        self.set.isRightHandType = NO;
    }
    else if ([self.handTypeLable.text isEqualToString:@"右手模式"]) {
        self.set.isRightHandType = YES;
    }
    
    // 把当前action状态通过Model存储
    self.set.isOpenReadRemind = self.readSwitch.on;
    
    // 找到路径
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *cacheFile = [sandBoxPath stringByAppendingPathComponent:@"Cartoon"];
    NSFileManager *manager = [NSFileManager defaultManager];
    
    // 如果在这个路径没有路径没有文件夹,就创建文件夹
    if (![manager fileExistsAtPath:cacheFile]) {
        [manager createDirectoryAtPath:cacheFile withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"Cartoon/config.plist"];
    NSLog(@"%@", docPath);
    // 归档
    [NSKeyedArchiver archiveRootObject:self.set toFile:docPath];
    
    ZGSMineViewController *mineVC = [[ZGSMineViewController alloc] init];
    [self.navigationController pushViewController:mineVC animated:YES];
}

// 设置阅读模式
- (void)changeReadTypeAction:(UISwitch *)mySwitch {
    
}

#pragma mark - tableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfrRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        ZGSOptionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"oReuse" forIndexPath:indexPath];
        cell.leftImageView.image = [UIImage imageNamed:@"iconfont-iconshanchuanniu(1).png"];
        cell.rightImageView.image = [UIImage imageNamed:@"iconfont-jiantou.png"];
        cell.titleLabel.text = @"清除历史记录";
        return cell;
    }
    else {
        ZGSOptionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"oReuse" forIndexPath:indexPath];
        cell.leftImageView.image = [UIImage imageNamed:@"iconfont-shanchu.png"];
        cell.rightImageView.image = [UIImage imageNamed:@"iconfont-jiantou.png"];
        cell.titleLabel.text = self.cacheSizeStr;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        UIAlertView *delHistoryAlert = [[UIAlertView alloc] initWithTitle:@"枫叶岛" message:@"是否删除历史记录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        delHistoryAlert.tag = 3001;
        [delHistoryAlert show];
    }
    else if (indexPath.row == 1) {
        UIAlertView *delHistoryAlert = [[UIAlertView alloc] initWithTitle:@"枫叶岛" message:@"是否删除缓存文件" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        delHistoryAlert.tag = 3002;
        [delHistoryAlert show];
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}



#pragma mark - setAction

// 设置惯用手
- (void)setHandTypeAction:(UITapGestureRecognizer *)tap {
    // 设置ActionSheet
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"惯用手设置" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"左手模式", @"右手模式", nil];
    // 把sheet 放在当前View上
    [sheet showInView:self.view];
}

#pragma mark - actionSheet协议方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSLog(@"buttonIndex ======= %ld", buttonIndex);
    NSLog(@"self.readSwitch.on ============= %d",self.readSwitch.on);
    // 左手0, 右手1
    if (buttonIndex == 0) {
        self.handTypeLable.text = @"左手模式";
    }
    else if (buttonIndex == 1) {
        self.handTypeLable.text = @"右手模式";
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
